package cn.samir.sudoku.db.dao;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import cn.samir.sudoku.core.Sudokus;
import cn.samir.sudoku.db.DBHelper;
import cn.samir.sudoku.util.BeanUtils;
import cn.samir.sudoku.util.LogUtils;
import cn.samir.sudoku.util.Utils;

public class SudokuDao {
	private static final String TAG = "SudokuDao";
	private DBHelper dbHepler;
	private final String table = "sudokus";
	Context context;
	public SudokuDao(Context context) {
		this.context = context;
		dbHepler = new DBHelper(context);
	}

	public int insert(Sudokus sudokus) {
		if (sudokus == null)
			return -1;
		SQLiteDatabase db = dbHepler.getWritableDatabase();
		if (db == null || !db.isOpen()) {
			return -1;
		}
		if ((query(sudokus.content) != null)) {
			LogUtils.d(TAG, "这个相同" + sudokus.toString());
			return 1;
		}

		return (int) db.insert(table, null, BeanUtils.BeanToValues(sudokus));
	}

	public Sudokus query(String content) {

		if (content == null)
			return null;
		SQLiteDatabase db = dbHepler.getReadableDatabase();
		if (db == null || !db.isOpen()) {
			return null;
		}
		Cursor c = db.query(table, null, " content=? ",
				new String[] { content }, null, null, null);
		if (c.moveToFirst()) {
			return BeanUtils.cursorToBean(c, Sudokus.class);
		}
		return null;
	}

	public int delete(Sudokus sudokus) {
		if (sudokus == null)
			return -1;
		SQLiteDatabase db = dbHepler.getWritableDatabase();
		if (db == null || !db.isOpen()) {
			return -1;
		}
		return db
				.delete(table, " content=? ", new String[] { sudokus.content });
	}

	public int update(Sudokus sudokus) {
		if (sudokus == null)
			return -1;
		SQLiteDatabase db = dbHepler.getWritableDatabase();
		if (db == null || !db.isOpen()) {
			return -1;
		}
		return (int) db.update(table, BeanUtils.BeanToValues(sudokus),
				" content=? ", new String[] { sudokus.content });
	}

	public ArrayList<Sudokus> queryAll() {
		SQLiteDatabase db = dbHepler.getReadableDatabase();
		if (db == null || !db.isOpen()) {
			return null;
		}
		ArrayList<Sudokus> sudokus = null;
		Cursor c = db.query(true, table, null, null, null, null, null, null,
				null);
		if (c != null) {
			sudokus = new ArrayList<Sudokus>();
			while (c.moveToNext()) {
				Sudokus sudoku = BeanUtils.cursorToBean(c, Sudokus.class);
				sudokus.add(sudoku);
			}
		}
		return sudokus;
	}

	public ArrayList<Sudokus> queryPassed(String code) {
		ArrayList<Sudokus> sudokus = null;
		SQLiteDatabase db = dbHepler.getReadableDatabase();
		if (db == null || !db.isOpen()) {
			return null;
		}
		Cursor c = db.query(true, table, null, " passed=? AND code = ? ",
				new String[] { "1", code}, null, null, null, null);
		if (c != null) {
			sudokus = new ArrayList<Sudokus>();
			while (c.moveToNext()) {
				Sudokus sudoku = BeanUtils.cursorToBean(c, Sudokus.class);
				sudokus.add(sudoku);
			}
		}
		return sudokus;
	}

	public ArrayList<Sudokus> queryWithArgs(String selection, String[] selectionargs) {
		ArrayList<Sudokus> sudokus = null;
		SQLiteDatabase db = dbHepler.getReadableDatabase();
		if (db == null || !db.isOpen()) {
			return null;
		}
		Cursor c = db.query(true, table, null, selection, selectionargs, null, null, null,
				null);
		if (c != null) {
			sudokus = new ArrayList<Sudokus>();
			while (c.moveToNext()) {
				Sudokus sudoku = BeanUtils.cursorToBean(c, Sudokus.class);
				sudokus.add(sudoku);
			}
		}
		return sudokus;
	}

}
